//
//  HMComPoseViewController.swift
//  WeiBo
//
//  Created by 李立达 on 15/12/15.
//  Copyright © 2015年 heima11. All rights reserved.
//

import UIKit




class HMComPoseViewController: UIViewController {
    //发微博文字的设置
    lazy var midleLabl :UILabel = {
        let lable = UILabel()
            lable.numberOfLines = 0
            if let name = HMusercount.shareuserCount.userAccount?.screen_name
            {
                let attrText = NSMutableAttributedString(string: "发微博\n\(name)")
                let rang = (attrText.string as NSString).rangeOfString(name)
                
                let atts = [NSFontAttributeName:UIFont.systemFontOfSize(14),NSForegroundColorAttributeName:UIColor.grayColor()]
            
                attrText.addAttributes(atts,range:rang)
                lable.attributedText = attrText
            }
            else
            {
                lable.text = "发微博"
            }
            lable.textAlignment = .Center
            lable.sizeToFit()
        return lable
    }()
    //rightButton的设置
    lazy var sendButton : UIButton = {
    
        let button = UIButton()
        button.setTitle("发送", forState: .Normal)
        button.setBackgroundImage(UIImage(named: "common_button_orange"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_orange_highlighted"), forState: UIControlState.Highlighted)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Highlighted)
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.width = 40
        button.height = 35
        return button
   }()
    //懒加载输入框
    lazy var textView : HMComposeTextView = {
        let lable = HMComposeTextView()
        lable.placeholder = "请输入你要发的微博"
        lable.font = UIFont.systemFontOfSize(16)
        return lable
    }()
    //懒加载底部toolbar
   lazy var  toolBar : HMComposeToolBar = HMComposeToolBar(frame: CGRectZero)
    lazy var pickerView : HMComposeCollectionView = {
        let picture = HMComposeCollectionView()
        
        return picture
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
       
            self.view.backgroundColor = UIColor.whiteColor()
            navigationItem.leftBarButtonItem = UIBarButtonItem( title: "返回", target: self, anction: "backButtonClick")
            navigationItem.rightBarButtonItem = UIBarButtonItem(customView: sendButton)
            navigationItem.titleView = midleLabl
            setupUI()
    }
    
    func setupUI(){
        view.addSubview(textView)
        view.addSubview(toolBar)
        textView.snp_makeConstraints { (make) -> Void in
          make.edges.equalTo(view).offset(UIEdgeInsetsZero)
        }
        
        toolBar.snp_makeConstraints { (make) -> Void in
            
            make.width.equalTo(view)
            make.bottom.equalTo(view)
            make.height.equalTo(44)
        }
        textView.addSubview(pickerView)
        
        pickerView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(textView).offset(100)
            make.centerX.equalTo(textView)
            make.width.equalTo(textView).offset(-20)
            make.height.equalTo(textView.snp_width).offset(-20)
            
        }
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardChange:", name: UIKeyboardWillChangeFrameNotification, object: nil)
        
        toolBar.buttomClick = { [weak self] (type:HMcomPosebutton) -> () in
        
            switch type{
            case .picture:
            self?.selectedPictrue()
            case .mention:
                print("picture")
            case .Trend:
                print("picture")
            case .Emotion:
                print("picture")
            case .Add:
                print("picture")
            
            }
            
        }
    }
    
   //keyboard的通知调用方法,用来监听键盘frame变化
   @objc func keyBoardChange(noti:NSNotification){
    
        let endFrom = (noti.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
    
        let duration = (noti.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! NSNumber)
        toolBar.snp_updateConstraints { (make) -> Void in
            
            make.bottom.equalTo(self.view).offset(endFrom.origin.y-self.view.height)
        }
        UIView.animateWithDuration(duration.doubleValue) { () -> Void in
            self.toolBar.layoutIfNeeded()
        }
    }
    deinit{
    NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    //返回按钮的点击方法
    func backButtonClick(){
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    func selectedPictrue(){
    
         let pickerVC = UIImagePickerController()
         pickerVC.allowsEditing = true
         pickerVC.delegate = self
        presentViewController(pickerVC, animated: true, completion: nil)
    }
  
}

extension HMComPoseViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
         pickerView.addimgaes(image)
         picker.dismissViewControllerAnimated(true, completion: nil)
        }
    
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        picker.dismissViewControllerAnimated(true, completion: nil)
    }
}
